package com.kaka.bus.engine.borrow.signed;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.engine.HomeActivity;
import com.kaka.bus.engine.borrow.pact.PactActivity;
import com.kaka.bus.engine.other.DrainageActivity;
import com.kaka.bus.manager.Result;
import com.kaka.bus.util.U;
import com.kaka.bus.util.logger.Logger;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;

/**
 * Created by zhangqi on 2016/10/21.
 */

public class SignedActivity extends BaseActivity<SignedPresenter> implements SignedContract.View {


    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.iv_signed_shenhe)
    ImageView mIvSignedShenhe;
    @BindView(R.id.iv_signed_pact)
    ImageView mIvSignedPact;
    @BindView(R.id.iv_signed_dakuan)
    ImageView mIvSignedDakuan;
    @BindView(R.id.tv_signed_tv1)
    TextView mTvSignedTv1;
    @BindView(R.id.tv_signed_xieyi)
    TextView mTvSignedXieyi;
    @BindView(R.id.ll_signed_successvisible)
    LinearLayout mLlSignedSuccessvisible;
    @BindView(R.id.tv_signed_look)
    TextView mTvSignedLook;
    @BindView(R.id.ll_signed_successgone)
    LinearLayout mLlSignedSuccessgone;
    @BindView(R.id.btn_signed_commit)
    AppCompatButton mBtnSignedCommit;

    public static final String SIGN_NOACTION = "NOACTION";
    public static final String SIGN_NOACTION_VALUE = "1";

    @Override
    protected void initView() {
        super.initView();
        mToolbarName.setText("审核");
        mToolbarBack.setOnClickListener(v -> finish());
        mBtnSignedCommit.setEnabled(false);
        mTvSignedLook.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        mTvSignedLook.getPaint().setAntiAlias(true);
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        String value = intent.getStringExtra(SIGN_NOACTION);
        if(SIGN_NOACTION_VALUE.equals(value)){
            return;
        }

        mPresenter.getSignedResult(getUserId(), getToken(),Constant.PHONE_SIGN);
    }

    @Override
    protected void initListener() {
        mBtnSignedCommit.setOnClickListener(v -> {
            startActivity(new Intent(this, PactActivity.class));
            finish();
            overridePendingTransition(R.anim.tran_in,R.anim.tran_out);
        });

        mTvSignedLook.setOnClickListener(v -> {
            finish();
        });
    }


    @Override
    public void validate(HttpWrapper<String> wrapper) {

        switch (wrapper.code) {
            case "208":
                mPresenter.NodeOk(getUserId(),getToken());
                break;
            case "207":
                addSubscription(Flowable.just(0)
                        .delay(180,TimeUnit.SECONDS)
                        .subscribeWith(new Result<Integer>() {
                            @Override
                            protected void onSuccess(Integer integer) {
                                mPresenter.getSignedResult(getUserId(),getToken(), Constant.PHONE_SIGN);
                            }
                        }));
                break;
            case "209":
                Logger.i("手机认证失败了");
                dissDialog();
                /**
                 * 失败
                 */
                U.toDrainage(this, DrainageActivity.TYPE_REFUSE,Constant.REFUES);
                showMessage(wrapper.info);
                break;
            case "210":
                Logger.i("手机认证黑名单");
                dissDialog();
                /**
                 * 失败
                 */
                U.toDrainage(this, DrainageActivity.TYPE_REFUSE,Constant.BLACK);
                break;
            case "201":
                Logger.i("201错误");
                dissDialog();
                showMessage(wrapper.info);
                break;
            case "301":
                dissDialog();
                U.tokenOut(this);
                break;
            case "211":
                U.getSingleDialog(this, "温馨提示", wrapper.getInfo(), "确定", () -> {
                    startActivity(new Intent(this,HomeActivity.class));
                    finish();
                });
                break;
        }
    }

    @Override
    public void NodeOk(HttpWrapper<String> wrapper) {
        if(Constant.SUCCESS_CODE.equals(wrapper.code)){
            success();
        }else if(Constant.ERROR_CODE.equals(wrapper.code)){
            showMessage(wrapper.info);
        }else if(Constant.TOKEN_TIMEOUT.equals(wrapper.code)){
            U.tokenOut(this);
        }else if(Constant.ID_CODE.equals(wrapper.code)){
            startActivity(new Intent(this,HomeActivity.class));
            finish();
        }
    }

    /**
     * 认证成功 弹窗
     */
    private void success() {
        mBtnSignedCommit.setEnabled(true);
        mLlSignedSuccessgone.setVisibility(View.GONE);
        mTvSignedXieyi.setVisibility(View.VISIBLE);
        mBtnSignedCommit.setBackgroundResource(R.drawable.button_ripple_blue);
        mBtnSignedCommit.setTextColor(Color.WHITE);
        mBtnSignedCommit.setText(getString(R.string.next));
        mTvSignedTv1.setText("恭喜您审核通过,请签署");
        mIvSignedShenhe.setImageResource(R.mipmap.borrowing_icon_shenhe);
        mIvSignedPact.setImageResource(R.mipmap.borrowing_icon_qianyue);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_signed;
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
